<?php

require_once dirname(__FILE__) . '/../lib/long_term_program_applicationGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/long_term_program_applicationGeneratorHelper.class.php';

/**
 * long_term_program_application actions.
 *
 * @package    sf_sandbox
 * @subpackage long_term_program_application
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class long_term_program_applicationActions extends autoLong_term_program_applicationActions {

    public function executeEdit(sfWebRequest $request) {
        $obj = LongTermProgramApplicationTable::getInstance()->find($request->getParameter('id'));
        $lpid = $obj->getLongProgramId();
        $teacher_obj = TeacherTable::getInstance()->find($obj->getTeacherId());
        $this->teacher_mobile = $teacher_obj->getMobile();
        $conditions = $request->getParameter('condition', false);
        $user_id = intval($teacher_obj->getIntgIdentity());
        $this->teacher_name = $teacher_obj->getFullName();
        $application_obj = $obj;

        if (is_object($application_obj))
            $this->form = new LongTermProgramApplicationForm($application_obj);

        $this->lock = false;

        $this->success = false;
        if ($request->isMethod('POST')) {
            $values = $request->getParameter($this->form->getName());
            $values['long_program_id'] = ($lpid);
            $values['teacher_id'] = ($user_id);
            $this->form->bind($values);
            if ($this->form->isValid()) {

                LinkLongProgramConditionTable::getInstance()->
                        createQuery()->delete()
                        ->where('teacher_id = ?', $user_id)->execute();

                if (count($conditions))
                    foreach ($conditions as $k => $one) {
                        $link = new LinkLongProgramCondition();
                        $link->set('teacher_id', $user_id);
                        $link->set('condition_id', $k);
                        $link->set('long_program_id', $lpid);
                        $link->set('answer', $one);
                        try {
                            $link->save();
                        } catch (Exception $e) {
                            
                        }
                    }
                if ($this->form->getObject()->isNew()) {
                    $this->success = 'save';
                } else {
                    $this->success = 'edit';
                }
                $long_app_obj = $this->form->save();

                /* ------------- */
                $prog_name = $request->getParameter('program_name');
                $prog_hours = $request->getParameter('program_hours');

                $prog_name_glue = implode(';', $prog_name);
                $prog_hours_glue = implode(';', $prog_hours);

                $long_app_obj->set('past_program_name', $prog_name_glue);
                $long_app_obj->set('past_program_hours', $prog_hours_glue);

                $long_app_obj->save();
                /* ------------- */

                if ($long_app_obj->getMobile() != $this->teacher_mobile) {
                    $teacher_obj->setMobile($long_app_obj->getMobile());
                    $teacher_obj->save();
                }

            } else {
                
            }
        }

        $this->conditions = Doctrine_Query::create()->from('LongProgramCondition')
                        ->where('long_program_id = ?', $lpid)->fetchArray();

        $selected_conditions = Doctrine_Query::create()->from('LinkLongProgramCondition')
                        ->where('long_program_id = ?', $lpid)->andWhere('teacher_id = ?', $user_id)->fetchArray();

        $out_conditions = array();

        foreach ($selected_conditions as $one)
            $out_conditions[$one['condition_id']] = $one['answer'];

        $this->selected_conditions = $out_conditions;
    }

    public function executeApprove($request) {
        $obj = $this->getRoute()->getObject();
        $obj->set('is_approved', true);
        $obj->save();
        $this->getUser()->setFlash('notice', 'long_application_approved_message');
        $this->redirect($request->getReferer());
    }

    public function executeUnApprove($request) {
        $obj = $this->getRoute()->getObject();
        $obj->set('is_approved', false);
        $obj->save();
        $this->getUser()->setFlash('notice', 'long_application_unapproved_message');
        $this->redirect($request->getReferer());
    }

}
